package com.ds.infrastructure.audit.client.factory;

import com.ds.infrastructure.audit.client.enums.KbaProtocol;
import com.ds.infrastructure.audit.client.track.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 作用：
 *
 * @author WeiShaoying
 * @date 2020/7/27
 */
public class KbaTrackFactory {

    /**
     * 获取默认的MQ客户端
     *
     * @param server
     * @param appName 注册时的应用名称
     * @param appKey  注册时的key
     * @return
     */
    public static KbaTrack getClient(String server, String appName, String appKey) {
        return getClient(KbaProtocol.MQ, server, appName, appKey);
    }

    /**
     * 通过Sever和protocol创建KbaTrack
     *
     * @param protocol
     * @param server
     * @return
     */
    public static AbstractKbaTrack getClient(KbaProtocol protocol, String server) {
        AbstractKbaTrack track;
        switch (protocol) {
            case HTTP:
                track = new KbaProtocolTrackHttp(server);
                break;
            case ASYNC_HTTP:
                track = new KbaProtocolTrackAsyncHttp(server);
                break;
            case MQ:
            case RPC:
            default:
                track = new KbaProtocolTrackMq(server);
                break;
        }
        Objects.requireNonNull(track, "Create KbaTrack Failed");
        track.initialize();
        return track;
    }

    /**
     * 通过Sever, appName, appKey, protocol创建KbaTrack
     *
     * @param protocol
     * @param server
     * @return
     */
    public static AbstractKbaTrack getClient(KbaProtocol protocol, String server, String appName, String appKey) {
        if (StringUtils.isBlank(appName) || StringUtils.isBlank(appKey)) {
            throw new IllegalArgumentException("appName,appKey不能为空");
        }
        AbstractKbaTrack track;
        switch (protocol) {
            case HTTP:
                track = new KbaProtocolTrackHttp(server, appName, appKey);
                break;
            case ASYNC_HTTP:
                track = new KbaProtocolTrackAsyncHttp(server, appName, appKey);
                break;
            case MQ:
            case RPC:
            default:
                track = new KbaProtocolTrackMq(server, appName, appKey);
                break;
        }
        Objects.requireNonNull(track, "Create KbaTrack Failed");
        track.initialize();
        return track;
    }

}
